package com.zygh.questionnairesurvey.controller;

import com.alibaba.fastjson.JSONObject;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.UserRole;
import com.zygh.questionnairesurvey.server.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/UserRole")
@Api(tags = "角色管理")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation(value = "查询所有角色")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", required = true)
    })
    public CommonResult listAll(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize) {
        return userRoleService.listAll(pageNum, pageSize);
    }


    @ApiOperation(value = "根据id查询角色信息")
    @GetMapping("getById")
    public CommonResult getById(Integer id) {
        return userRoleService.getById(id);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("insert")
    public CommonResult insert(@RequestBody UserRole userRole) {
        return userRoleService.insert(userRole);
    }

    @ApiOperation(value = "修改角色")
    @PostMapping("update")
    public CommonResult update(@RequestBody UserRole userRole) {
        return userRoleService.update(userRole);
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("delete")
    public CommonResult delete(@RequestBody String id) {
        String str = JSONObject.parseObject(id).get("id").toString();
        Integer new_id = Integer.parseInt(str);
        return userRoleService.delete(new_id);
    }

}